﻿// ----------------------------------------------------------------------------------------
// Author:                    Magaly Vargas y Joaquín López
// Company:                   ALBALIA INTERACTIVA S.L.
// Assembly version:          1.0.0.0
// Date:                      22/02/2010
// Time:                      12:00
// Solution Name:             OffInvoiceWordAddIn
// Solution Filename:         OffInvoiceWordAddIn
// Solution FullFilename:     C:\Desarrollo\OffInvoiceWordAddIn
// Project Name:              OffInvoiceWordAddIn
// Project Filename:          OffInvoiceWordAddIn.csproj
// Project FullFilename:      C:\Desarrollo\OffInvoiceWordAddIn\OffInvoiceWordAddIn.csproj
// Project Item Name:         FrmTimeStampConfig.cs
// Project Item Filename:     FrmTimeStampConfig.cs
// Project Item FullFilename: C:\Desarrollo\OffInvoiceWordAddIn\FrmTimeStampConfig.cs
// Project Item Kind:         Código
// Purpose:                   
//Copyright © 2009 - 2010 Albalia Interactiva S.L. Reservados todos los derechos.
// ----------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OffInvoiceWordAddIn
{
    public partial class FrmTimeStampConfig : Form
    {
        public TimeStampConfig TstConfig { get; set; }
        public bool Save { get; set; }
        public TimeStamp defaultTst { get; set; }

        public FrmTimeStampConfig()
        {
            InitializeComponent();
            Save = false;
            TstConfig = null;
            defaultTst = null;
        }

        private void ckbLogOnTimeStamp_CheckedChanged(object sender, EventArgs e)
        {
            if (ckbLogOnTimeStamp.Checked)
            {
                enableUserData(true);
            }
            else
            {
                txtUserNameTimeStamp.Text = string.Empty;
                txtPasswordTimeStamp.Text = string.Empty;
                enableUserData(false);

            }
        }

        private void enableUserData(bool enable)
        {
            lbUserNameTimeStamp.Enabled = enable;
            lbPasswordTimeStamp.Enabled = enable;
            txtUserNameTimeStamp.Enabled = enable;
            txtPasswordTimeStamp.Enabled = enable;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Save = false;
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (valido())
            {
                if (defaultTst == null)
                {
                    defaultTst = new TimeStamp();
                    defaultTst.Name = txtNameTimeStamp.Text;
                    defaultTst.Url = txtUrlTimeStamp.Text;
                    defaultTst.UserName = txtUserNameTimeStamp.Text;
                    defaultTst.Password = txtPasswordTimeStamp.Text;
                    TstConfig.TimeStamp.Add(defaultTst);
                }
                else
                {
                    for (int i = 0; i < TstConfig.TimeStamp.Count; i++)
                    {
                        if (TstConfig.TimeStamp[i].Equals(defaultTst))
                        {
                            TstConfig.TimeStamp[i].Name = txtNameTimeStamp.Text;
                            TstConfig.TimeStamp[i].Url = txtUrlTimeStamp.Text;
                            TstConfig.TimeStamp[i].UserName = txtUserNameTimeStamp.Text;
                            TstConfig.TimeStamp[i].Password = txtPasswordTimeStamp.Text;
                        }
                    }

                }
                Save = true;
                this.Close();
            }
        }

        private void FrmTimeStampConfig_Load(object sender, EventArgs e)
        {
            LoadLanguage();
            if (defaultTst != null)
            {
                btnRemoveTst.Visible = true;
                txtNameTimeStamp.Text = defaultTst.Name;
                txtUrlTimeStamp.Text = defaultTst.Url;
                if (!(String.IsNullOrEmpty(defaultTst.UserName)) && !(String.IsNullOrEmpty(defaultTst.UserName)))
                {
                    ckbLogOnTimeStamp.Checked = true;
                    txtUserNameTimeStamp.Text = defaultTst.UserName;
                    txtPasswordTimeStamp.Text = defaultTst.Password;
                }
            }
        }


        /// <summary>
        /// Método que carga el archivo de idioma y establece los textos de los controles
        /// en el idioma que corresponde.
        /// </summary>
        public void LoadLanguage()
        {
            try
            {
                Language labels = Language.getInstance();
                this.Text = labels.getLabels(this.Name);

                lbNameTimeStamp.Text = labels.getLabels(lbNameTimeStamp.Name);
                lbPasswordTimeStamp.Text = labels.getLabels(lbPasswordTimeStamp.Name);
                lbUrlTimeStamp.Text = labels.getLabels(lbUrlTimeStamp.Name);
                lbUserNameTimeStamp.Text = labels.getLabels(lbUserNameTimeStamp.Name);
                btnCancel.Text = labels.getLabels(btnCancel.Name);
                btnOk.Text = labels.getLabels(btnOk.Name);
                btnRemoveTst.Text = labels.getLabels(btnRemoveTst.Name);
                ckbLogOnTimeStamp.Text = labels.getLabels(ckbLogOnTimeStamp.Name);
                gbSettingTimeStampServer.Text = labels.getLabels(gbSettingTimeStampServer.Name);
            }
            catch (Exception)
            {
                ControlMessages msg = ControlMessages.getInstance();
                MessageBox.Show(msg.getMessage(ControlMessages.Msg.ErrorLoadLanguage), msg.getMessage(ControlMessages.Msg.CaptionConfig), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

        private bool valido()
        {
            ControlMessages msg = ControlMessages.getInstance();
            if (String.IsNullOrEmpty(txtNameTimeStamp.Text))
            {
                MessageBox.Show(msg.getMessage(ControlMessages.Msg.ErrorNameTimeStamp), msg.getMessage(ControlMessages.Msg.CaptionSTS), MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (String.IsNullOrEmpty(txtUrlTimeStamp.Text))
            {
                MessageBox.Show(msg.getMessage(ControlMessages.Msg.ErrorURLTimeStamp), msg.getMessage(ControlMessages.Msg.CaptionSTS), MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (ckbLogOnTimeStamp.Checked)
            {
                if (String.IsNullOrEmpty(txtUserNameTimeStamp.Text))
                {
                    MessageBox.Show(msg.getMessage(ControlMessages.Msg.ErrorUserTimeStamp), msg.getMessage(ControlMessages.Msg.CaptionSTS), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (String.IsNullOrEmpty(txtPasswordTimeStamp.Text))
                {
                    MessageBox.Show(msg.getMessage(ControlMessages.Msg.ErrorPasswordTimeStamp), msg.getMessage(ControlMessages.Msg.CaptionSTS), MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            return true;
        }

        private void btnRemoveTst_Click(object sender, EventArgs e)
        {
            ControlMessages msg = ControlMessages.getInstance();
            try
            {
                if (MessageBox.Show(msg.getMessage(ControlMessages.Msg.MsgRemoveTimeStamp), msg.getMessage(ControlMessages.Msg.CaptionSTS), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    TstConfig.TimeStamp.Remove(defaultTst);
                    defaultTst.Url = string.Empty;
                    Save = true;
                    this.Close();
                }
            }
            catch (Exception)
            {
            }

        }
    }
}
